﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Bule.Malbilden.Data;
using Bule.Malbilden.Domain;

namespace Bule.Malbilden.Forms
{
    public partial class UserView : Form
    {
        IPersistentContext context;

        public UserView(IPersistentContext context)
        {
            InitializeComponent();

            this.context = context;

            UpdateUserList();

            if (userList.Items.Count > 0)
                userList.SelectedIndices.Add(0);
        }

        public Person SelectedUser
        {
            get
            {
                if (userList.SelectedItems.Count > 0)
                    return (Person)userList.SelectedItems[0].Tag;

                return null;
            }
        }

        private void UpdateUserList()
        {
            userList.BeginUpdate();
            userList.Items.Clear();

            var persons = context.GetAll<Person>();

            foreach (Person p in persons)
            {
                var item = new ListViewItem();
                item.Text = p.Name;
                item.Tag = p;
                userList.Items.Add(item);
            }

            userList.EndUpdate();

            UpdateButtonEnabledState();
        }

        private void userList_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateButtonEnabledState();
        }

        private void UpdateButtonEnabledState()
        {
            bool isUserSelected = userList.SelectedItems.Count > 0;

            removeUserButton.Enabled = isUserSelected;
            okButton.Enabled = isUserSelected;
        }

        private void createNewUserButton_Click(object sender, EventArgs e)
        {
            AddUserForm addForm = new AddUserForm();
            DialogResult result = addForm.ShowDialog();

            if (result == DialogResult.OK)
            {
                try
                {
                    context.Save(new Person(addForm.UserName, addForm.Age,
                                            addForm.IsFemale, addForm.IsNursing,
                                            addForm.IsPregnant));
                    UpdateUserList();
                }
                catch (NHibernate.HibernateException ex)
                {
                    MessageBox.Show("Användaren kunde ej skapas!" + " " + ex, "Fel", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void removeUserButton_Click(object sender, EventArgs e)
        {
            if (userList.SelectedItems.Count > 0)
            {
                context.Remove((Person)userList.SelectedItems[0].Tag);
                UpdateUserList();
            }
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void userList_DoubleClick(object sender, EventArgs e)
        {
            if (userList.SelectedItems.Count > 0)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void userList_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            UpdateButtonEnabledState();
        }
    }
}